//
//  AboutUsViewController.m
//  Trucks
//
//  Created by enzhoukeji on 2018/7/12.
//  Copyright © 2018年 puyue.com. All rights reserved.
//

#import "VersionInformationViewController.h"

@interface VersionInformationViewController ()
@property (nonatomic ,strong) WKWebView *webView;
@property (nonatomic,strong) UIProgressView *progressView;

@end

@implementation VersionInformationViewController
//懒加载
- (WKWebView *)webView
{
    
    if (_webView == nil) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kMainScreen_width, kMainScreen_height-kMainTopHeight)];
        _webView.scrollView.bounces = NO;
        [self.view addSubview:_webView];
    }
    return _webView;
}
-(UIProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.frame = CGRectMake(0, 0, self.webView.frame.size.width, 2);
        _progressView.trackTintColor = [UIColor whiteColor]; // 设置进度条的色彩
        _progressView.progressTintColor = [UIColor colorWithRed:41/255.0 green:107/255.0 blue:255/255.0 alpha:1];
        // 设置初始的进度，防止用户进来就懵逼了（微信大概也是一开始设置的10%的默认值）
        [_progressView setProgress:0.1 animated:YES];
        [self.webView addSubview:_progressView];
    }
    return _progressView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNavi];
    [self.view addSubview:self.webView];
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.H5strUrl]]];
    [self.navigationController.navigationBar setBackgroundImage:[self buttonImageFromColor:[UIColor colorWithRed:46/255.0 green:54/255.0 blue:77/255.0 alpha:1]] forBarMetrics:UIBarMetricsDefault];
    self.title = self.titleName;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.webView.estimatedProgress;
        if (self.progressView.progress == 1) {
            /*
             *添加一个简单的动画，将progressView的Height变为1.4倍，在开始加载网页的代理中会恢复为1.5倍
             *动画时长0.25s，延时0.3s后开始动画
             *动画结束后将progressView隐藏
             */
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
                
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
- (void)dealloc {
    // 最后一步：移除监听
    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
#pragma mark---创建导航条
- (void)createNavi{
    self.view.backgroundColor = [UIColor whiteColor];
    //    左侧按钮
    UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonLeft.frame = CGRectMake(0, 0, 20, 20);
//    //判断当前系统版本
//    if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) {
//        buttonLeft.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 20);
//    }
    [buttonLeft addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [buttonLeft setImage:[UIImage imageNamed:@"jiantouhui"] forState:UIControlStateNormal];
    buttonLeft.tag = 200;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:buttonLeft];
    //将整个viewBackInNavi右移10
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    negativeSpacer.width =5;//负数为右移，正数为左移
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
}
//通过颜色来生成一个纯色图片
- (UIImage *)buttonImageFromColor:(UIColor *)color{
    CGRect rect = CGRectMake(0, 0, WIDTH, SafeAreaTopHeight);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
-(void)buttonClick{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
